package com.coocaa.ops.admin.v1.homepage.convert;

import com.coocaa.ops.common.core.biz.homepage.entity.HomeModuleContentConfigInfo;
import com.coocaa.ops.common.core.biz.homepage.entity.HomepageInfoEntity;
import com.coocaa.ops.common.core.biz.policygroup.entity.PolicyGroupChipModelConfigEntity;
import com.coocaa.ops.common.core.biz.policygroup.entity.PolicyGroupInfoEntity;
import com.coocaa.ops.common.tool.base.InstanceUtil;
import lombok.experimental.UtilityClass;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
 * @author bijiahao
 * @description
 * @date 2020/6/14 17:28
 */
@UtilityClass
public class HomeQueryConverter {

    public static void handleSetRlsPolicyName(List<PolicyGroupInfoEntity> rlsPolicyList, HomepageInfoEntity entity){
        StringBuilder policysInfo = new StringBuilder();
        for (PolicyGroupInfoEntity policyConfEntity : rlsPolicyList) {
            policysInfo.append(policyConfEntity.getPolicyGroupName())
                    .append("(")
                    .append(policyConfEntity.getId())
                    .append(")")
                    .append(",");


        }
        String relationPolicys = policysInfo.toString();
        // 去掉最后的“，”号
        if (relationPolicys.endsWith(",")) {
            relationPolicys = relationPolicys.substring(0, relationPolicys.length() - 1);
        }

        if (StringUtils.isNotBlank(relationPolicys)) {
            // 如果不为空，就设置
            entity.setRelationPolicyName(relationPolicys);
        } else {
            // 如果为空， 则这样设置
            entity.setRelationPolicyName("--");
        }
    }

    public static void handleSetRlsChiModelName(List<PolicyGroupChipModelConfigEntity> rlsChipModelList, HomepageInfoEntity entity){
        StringBuilder chipModelInfos = new StringBuilder();
        for (PolicyGroupChipModelConfigEntity policyConfEntity : rlsChipModelList) {
            chipModelInfos.append(policyConfEntity.getModel())
                    .append("_")
                    .append(policyConfEntity.getChip())
                    .append(",");


        }
        String chipModel = chipModelInfos.toString();
        if (chipModel.endsWith(",")) {
            chipModel = chipModel.substring(0, chipModel.length() - 1);
        }
        if (StringUtils.isNotBlank(chipModel)) {
            entity.setChipModel(chipModel);
        } else {
            entity.setChipModel("--");
        }
    }


    public static List<HomeModuleContentConfigInfo> convertFinalModuleList(List<HomeModuleContentConfigInfo> commonModuleList, List<HomeModuleContentConfigInfo> dmpModuleList){
        TreeMap<Integer, HomeModuleContentConfigInfo> tabPanelConfigInfoTreeMap  = InstanceUtil.newTreeMap();
        for (HomeModuleContentConfigInfo contentConfigInfo : commonModuleList){
            contentConfigInfo.setModuleIsDmp(0);
            tabPanelConfigInfoTreeMap.put(contentConfigInfo.getSeq(), contentConfigInfo);
        }
        if(CollectionUtils.isNotEmpty(dmpModuleList)){
            handleFilterDmp(tabPanelConfigInfoTreeMap, dmpModuleList);
        }
        return convert2List(tabPanelConfigInfoTreeMap);

    }

    private static  List<HomeModuleContentConfigInfo>  convert2List(TreeMap<Integer, HomeModuleContentConfigInfo> tabPanelConfigInfoTreeMap ){
        List<HomeModuleContentConfigInfo> tabPanelConfigInfoList = InstanceUtil.newArrayList();
        for (Map.Entry<Integer, HomeModuleContentConfigInfo> entry : tabPanelConfigInfoTreeMap.entrySet()){
            tabPanelConfigInfoList.add(entry.getValue());
        }
        return tabPanelConfigInfoList;
    }

    private static void handleFilterDmp( TreeMap<Integer, HomeModuleContentConfigInfo> tabPanelConfigInfoTreeMap , List<HomeModuleContentConfigInfo> dmpModuleList){
        for (HomeModuleContentConfigInfo dmpModule : dmpModuleList){
            Integer seq = dmpModule.getSeq();
            tabPanelConfigInfoTreeMap.put(seq, dmpModule);
            dmpModule.setModuleIsDmp(1);
        }
    }
}
